﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using Runda.CMS.Order.Entity;
using Runda.CMS.Common.Utility;
using System.Linq.Expressions;
using System.Data.Linq.Mapping;
using System.Data.Linq;

namespace Runda.CMS.Order.Repository
{
    public class AccountRepository
    {
        private string db = ConfigurationManager.ConnectionStrings["OrderConnectionString"].ConnectionString;

        public Account findAccountByCustomerID(String customerID) { 
            using (OrderDataContext context = new OrderDataContext(db)){
                bool isExist = context.account.Any(account => account.customerID == customerID);
                if (isExist)
                {
                    Account at = context.account.Single(account => account.customerID == customerID);
                    return at;
                }
                return null;
            }
        }

        public Account findAccountByAccountName(String accountName)
        {
            using (OrderDataContext context = new OrderDataContext(db))
            {
                bool isExist = context.account.Any(account => account.accountName == accountName);
                if (isExist)
                {
                    Account at = context.account.Single(account => account.accountName == accountName);
                    return at;
                }
                return null;
            }
        }
        
        public List<Account> findAccountByNameAndPassword(String accountName, String password) {
            using (OrderDataContext context = new OrderDataContext(db))
            {
                String encordPassword = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5");
                var od = from account in context.account
                         where account.accountName == accountName &&
                             account.password == encordPassword
                         select account;
                return od.ToList<Account>();
            }
        }

        public Account findAccountById(Guid id)
        {
            using (OrderDataContext context = new OrderDataContext(db))
            {
                bool isExist = context.account.Any(account => account.AccountID == id);
                 if (isExist)
                 {
                     Account at = context.account.Single(account => account.AccountID == id);
                     return at;
                 }
                 return null;
            }
        }

        public void persist(Account account) {
            using (OrderDataContext context = new OrderDataContext(db))
            {
                context.account.InsertOnSubmit(account);
                context.SubmitChanges();
            }
        }

        public void merge(Account account) {
            using (OrderDataContext context = new OrderDataContext(db))
            {
                context.account.Attach(account);
                context.Refresh(RefreshMode.KeepCurrentValues, account);
                context.SubmitChanges();
            }
        }
    }
}
